package com.buba.system.controller;

import com.buba.system.pojo.*;
import com.buba.system.service.EmployeeService;
import com.buba.system.service.JobService;
import com.buba.system.service.LoginService;
import com.buba.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录
 */
@RestController
@RequestMapping("/LoginController")
public class LoginController {
    @Autowired
    @Qualifier("loginServiceImpl")
    private LoginService loginService;
    @Autowired
    @Qualifier("employeeServiceImpl")
    private EmployeeService employeeService;
    @Autowired
    @Qualifier("jobServiceImpl")
    private JobService jobService;
    @Autowired
    @Qualifier("menuServiceImpl")
    private MenuService menuService;

    @RequestMapping("/login")
    public Map<String, Object> login(SystemUser systemUser, HttpSession session, HttpServletRequest request) throws UnknownHostException {
        SystemUser loginUser = loginService.login(systemUser);
        Map<String, Object> map = new HashMap<>();
        if (loginUser == null) {
            LoginLog loginLog= new LoginLog();
            String employeeId=loginService.selLoginByEmployeeId(systemUser);
            // 当前登录到用户id
            loginLog.setEmployeeId(employeeId);
            // 用户登录的ip
            InetAddress ia=InetAddress.getLocalHost();
            String localip=ia.getHostAddress();
            loginLog.setIp(localip);
            // 登录时间
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            loginLog.setLoginTime(df.format(new Date()));
            // 登录类型失败0 成功为1
            loginLog.setLogin_type(0);
            // 调用登录日志业务层
            loginService.addLoginLog(loginLog);
            map.put("message", "帐号或密码错误");
            return map;
        } else {
            LoginLog loginLog= new LoginLog();
            String employeeId=loginService.selLoginByEmployeeId(systemUser);
            // 当前登录到用户id
            loginLog.setEmployeeId(employeeId);
            // 用户登录的ip
            InetAddress ia=InetAddress.getLocalHost();
            String localip=ia.getHostAddress();
            loginLog.setIp(localip);
            // 登录时间
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            loginLog.setLoginTime(df.format(new Date()));
            // 登录类型失败0 成功为1
            loginLog.setLogin_type(1);
            // 调用登录日志业务层
            loginService.addLoginLog(loginLog);
            // 查询该用户拥有的菜单权限以及个人信息。
            // 根据登录成功帐号的员工外键，根据外键查询信息
            Employee employee = employeeService.selEmployee(loginUser.getEmployeeId());
            employee.setSystemUser(loginUser);
            // 根据用户id查询职位
            List<Job> jobList = jobService.selJob(employee.getEmployeeId());
            // 根据职位id查询拥有的菜单，
            // 当一个员工出现多个职位，如何解决菜单重复问题
            // 如何使用vue展示层级菜单（给Vue的data就是层级data）
            for (Job job : jobList) {
                List<Menu> menuList = menuService.selMenu(job.getJobId());
                job.setMenuList(menuList);
            }
            // 根据职位列表获取该用户所拥有的菜单集（去重复菜单、
            // 获得菜单层级效果）
            List<Menu> menuList = menuService.noDup(jobList);
            employee.setJobList(jobList);
            employee.setMenuList(menuList);
            session.setAttribute("employee", employee);
            map.put("message", "成功");
            return map;
        }
    }
    @RequestMapping("selLoginLog")
    public Page selLoginLog(@RequestParam Map<String, Object> map, String employeeId){
        Page page = new Page((Integer.parseInt(map.get("pageNow").toString())),(Integer.parseInt(map.get("pageCount").toString())));
        map.put("page", page);
        map.put("employeeId", employeeId);
        List<LoginLog> list = null;
        if (employeeId==null || employeeId=="") {
            //查询客户总条数b
            page.computePageTotal(loginService.selLoginLogCount());
            //查询所有客户信息
            list = loginService.selLoginLog(page);
        } else { // 携带查询条件
            //查询客户总条数b
            page.computePageTotal(loginService.selLoginLogCountByName(map));
            //查询所有客户信息
            list = loginService.selLoginLogByName(map);
        }
        page.setList(list);
        return page;
    }

}
